package com.bike.util;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import net.sf.json.JSONObject;

/**
 * 生成下拉框
 * 
 * @author lijiahui
 */
public class OptionsTag extends TagSupport {

	LinkedHashMap<String, Map<String, String>> codes = new LinkedHashMap<String, Map<String, String>>();
	/**
	 * 代码表定义编号
	 */
	private String key = null;

	/**
	 * 样式
	 */
	private String style = null;

	/**
	 * 样式class
	 */
	private String styleClass = null;

	/**
	 * 是否需要空值
	 */
	private String needNull = "true";

	/**
	 * 初始化即需显示的值
	 */
	private String selectedID = null;

	public LinkedHashMap<String, Map<String, String>> getCodes() {
		return codes;
	}

	public void setCodes(LinkedHashMap<String, Map<String, String>> codes) {
		this.codes = codes;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getNeedNull() {
		return needNull;
	}

	public void setNeedNull(String needNull) {
		this.needNull = needNull;
	}

	public String getSelectedID() {
		return selectedID;
	}

	public void setSelectedID(String selectedID) {
		this.selectedID = selectedID;
	}

	public int doStartTag() throws JspException {
		try {
			//codes = CacheSystemUtils.getDictMapByParentCode(getKey());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public int doEndTag() throws JspException {
		StringBuffer sb = new StringBuffer();
		if (needNull == "true") {
			addOption(sb, "", "--请选择--", false);
		}
		Iterator<Entry<String, Map<String, String>>> codeIterator = getCodes().entrySet().iterator();
		while (codeIterator.hasNext()) {
			Map.Entry entry = codeIterator.next();
			JSONObject codeObject = JSONObject.fromObject(entry.getValue());
			if (codeObject.getString("isdelete").equals("0")) {
				if (!codeObject.getString("code").equals(getKey())) {
					addOption(sb, codeObject.getString("code"), codeObject.getString("showdesc"), isMatched(codeObject.getString("code")));
				}
			}
		}
		ServletResponse response = pageContext.getResponse();
		try {
			pageContext.getOut().flush();
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html");
			response.getWriter().write(sb.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return EVAL_PAGE;
	}

	protected void addOption(StringBuffer sb, String value, String label, boolean matched) {
		sb.append("<option value=\"");
		sb.append(value);
		sb.append("\"");
		if (matched) {
			sb.append(" selected=\"selected\"");
		}
		if (style != null) {
			sb.append(" style=\"");
			sb.append(style);
			sb.append("\"");
		}
		if (styleClass != null) {
			sb.append(" class=\"");
			sb.append(styleClass);
			sb.append("\"");
		}
		sb.append(">");
		sb.append(label);
		sb.append("</option>\r\n");
	}

	/**
	 * 判断是否被选中
	 * 
	 * @param value
	 * @return
	 */
	private boolean isMatched(String value) {
		boolean matched = false;
		if ((selectedID == null) || (value == null)) {
			return matched;
		}
		String[] ids = selectedID.split(",");
		for (int i = 0; i < ids.length; i++) {
			if (ids[i].equals(value)) {
				matched = true;
				break;
			}
		}
		return matched;
	}

}
